package utils

import (
	"encoding/binary"
	"fmt"
	"os"
)

// MakeDirNotExist 不存在目录则创建
func MakeDirNotExist(dir string) error {
	stat, err := os.Stat(dir)
	if err != nil {
		if os.IsNotExist(err) {
			err = os.MkdirAll(dir, os.ModePerm)
			if err != nil {
				return err
			}
		}
	} else {
		if !stat.IsDir() {
			return fmt.Errorf("%s is not a directory", dir)
		}
	}

	return err
}

func BytesToInt64BigEndian(data []byte) int64 {
	return int64(BytesToUint64BigEndian(data))
}

func Int64ToBytesBigEndian(value int64) []byte {
	return Uint64ToBytesBigEndian(uint64(value))
}

func Uint64ToBytesBigEndian(n uint64) []byte {
	bytes := make([]byte, 8)
	binary.BigEndian.PutUint64(bytes, n)
	return bytes
}

func BytesToUint64BigEndian(data []byte) uint64 {
	return binary.BigEndian.Uint64(data)
}
